<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>库存查询</title>
<script type="text/javascript">
    Ext.onReady(function () {

        //产品
        var productCBB = Ext.create('Ext.form.ComboBox', {
            store: productStore,
            fieldLabel: '产品',
            emptyText: '请选择...',
            valueField: 'id',
            displayField: 'name',
            name: 'product_id',
            loadingText: '搜索中...',
            anchor: '100%',
            pageSize: 10,
            minChars: 0,
            listConfig: {
                loadingText: 'Searching...',
                getInnerTpl: function () {
                    return '<div style="float:left; width:230px" data-qtip="{name}">{name:ellipsis(26)}</div><div style="float:left; width:110px">{number}</div>{is_suit}';
                }
            }
        });

        //所有仓库
        var warehouse = Ext.create('Ext.form.ComboBox', {
            store: warehouseStore,
            fieldLabel: '仓库',
            emptyText: '请选择...',
            valueField: 'id',
            displayField: 'name',
            name: 'warehouse_id',
            loadingText: '搜索中...',
            anchor: '100%',
            //pageSize: 10,
            minChars: 0,
            editable: false
        });


        var orderTypeArray = Ext.create('Ext.form.ComboBox', {
            fieldLabel: '订单类型',
            store: orderTypeArrayStore,
            displayField:'text',
            typeAhead: true,
            queryMode: 'local',
            name: 'order_type',
            editable:false,
            valueField: 'value',
            anchor: '100%'
        });

        //产品类型
        var productType = Ext.create('Ext.form.ComboBox', {
            store: productTypeStore,
            fieldLabel: '产品类别',
            valueField: 'id',
            displayField: 'name',
            name: 'product_type_id',//如果不想提交displayField，则在这儿指定要提交的Key，value就是valueField．
            emptyText: '请选择...',
            loadingText: '搜索中...',
            anchor: '100%',
            readOnly: false,
            multiSelect: true,
            minChars: 0
        });

        var showBatchInfo = Ext.create('Ext.form.field.Checkbox', {
            name: 'batch_info',
            clearCls: 'allow-float',
            itemCls: 'float-left',
            fieldLabel: '显示批次'
        })

        var beginDate = Ext.create('Ext.form.field.Date', {
            fieldLabel: '起始日期',
            name: 'begin_date',
            id: 'begin_date',
            format: 'Y-m-d', //格式化日期
            //disabledDays: [0,6],	//不让选择周六，周日
            //disabledDaysText: "周末要休息",
            invalidText: "您输入的日期格式不对，正确格式为2010-01-01",
            anchor: '100%',
            vtype: 'daterange',
            endDateField: 'end_date'
        });

        var endDate = Ext.create('Ext.form.field.Date', {
            fieldLabel: '截止日期',
            name: 'end_date',
            id: 'end_date',
            format: 'Y-m-d', //格式化日期
            //disabledDays: [0,6],	//不让选择周六，周日
            //disabledDaysText: "周末要休息",
            invalidText: "您输入的日期格式不对，正确格式为2010-01-01",
            anchor: '100%',
            vtype: 'daterange',
            startDateField: 'begin_date'
        });

        var companies = Ext.create('Ext.form.ComboBox', {
            fieldLabel: '所属公司',
            store: companyAllInfoStore,
            allowBlank: true,
            emptyText: '请选择...',
            valueField: 'id',
            displayField: 'name',
            name: 'company_id',
            loadingText: '搜索中...',
            editable: false,
            minChars: 0,
            anchor: '100%'
        });

        var brand = Ext.create('Ext.form.ComboBox', {
            store: brandStore,
            fieldLabel: '品牌',
            allowBlank: true,
            valueField: 'id',
            displayField: 'name',
            name: 'brand_id',//如果不想提交displayField，则在这儿指定要提交的Key，value就是valueField．
            emptyText: '请选择...',
            editable: false,
            loadingText: '搜索中...',
            anchor: '100%',
            minChars: 0
        });

        var searchButton = Ext.create('Ext.Button', {
            text: '搜索',
            iconCls: 'finding',
            handler: function () {
                var productTypeIds = "";
                for (var i = 0; i < productType.getSubmitValue().length; i++) {
                    productTypeIds = productTypeIds + productType.getSubmitValue()[i];
                    if ((i + 1) != productType.getSubmitValue().length) {
                        productTypeIds = productTypeIds + ",";
                    }
                }
                stockDS.removeAll();
                stockDS.getProxy().extraParams.warehouse_id = warehouse.getValue();
                stockDS.getProxy().extraParams.batch_info = showBatchInfo.getValue();
                stockDS.getProxy().extraParams.begin_date = beginDate.getRawValue();
                stockDS.getProxy().extraParams.end_date = endDate.getRawValue();
                stockDS.getProxy().extraParams.order_type = orderTypeArray.getValue();
                stockDS.getProxy().extraParams.product_id = productCBB.getValue();
                stockDS.getProxy().extraParams.brand_id = brand.getValue();
                stockDS.getProxy().extraParams.company_id = companies.getValue();
                stockDS.getProxy().extraParams.product_type_id = productTypeIds;
                //stockDS.getProxy().extraParams.start=0
                toolBar.moveFirst({callback: function (record, options, success) {
                    if (financialManagement === '是' && options.response) {
                        costAmountLabel.setText("成本总值:" + Ext.util.Format.number(Ext.JSON.decode(options.response.responseText).sum_cost_amount, '0,000.00'));
                        taxAmountLabel.setText("成本总值(含税):" + Ext.util.Format.number(Ext.JSON.decode(options.response.responseText).sum_tax_amount, '0,000.00'));
                    }
                }});
                /*stockDS.load({callback: function (record, options, success) {
                    if (financialManagement === '是' && options.response) {
                        costAmountLabel.setText("成本总值:" + Ext.util.Format.number(Ext.JSON.decode(options.response.responseText).sum_cost_amount, '0,000.00'));
                        taxAmountLabel.setText("成本总值(含税):" + Ext.util.Format.number(Ext.JSON.decode(options.response.responseText).sum_tax_amount, '0,000.00'));
                    }
                }});*/
            }
        });

        var clearButton = Ext.create('Ext.Button', {
            text: '清空',
            width: 50,
            handler: function () {
                queryFormPanel.form.reset();
                //orderNumber.setValue('');
            }
        });

        var exportXLSButton = Ext.create('Ext.Button', {
            text: '导出为Excel文件',
            iconCls: 'xls',
            handler: function () {
                if (queryFormPanel.form.isValid()) {

                    var productTypeIds = "";
                    for (var i = 0; i < productType.getSubmitValue().length; i++) {
                        productTypeIds = productTypeIds + productType.getSubmitValue()[i];
                        if ((i + 1) != productType.getSubmitValue().length) {
                            productTypeIds = productTypeIds + ",";
                        }
                    }

                    var queryStr = "/stock/productQuery?sub_type=excel&";
                    queryStr += "warehouse_id=" + warehouse.getValue();
                    queryStr += "&order_type=" + orderTypeArray.getValue();
                    queryStr += "&product_id=" + productCBB.getValue();
                    queryStr += "&product_type_id=" + productTypeIds;
                    queryStr += "&batch_info=" + showBatchInfo.getValue();
                    queryStr += "&begin_date=" + beginDate.getRawValue();
                    queryStr += "&end_date=" + endDate.getRawValue();
                    window.open(queryStr, '_blank', 'width=1,height=1,toolbar=no,menubar=no,location=no');
                }
            }
        });

        var queryFormPanel = Ext.create('Ext.form.Panel', {
            fieldDefaults: {
                labelWidth: 55,
                labelAlign: 'right'
            },
            width: screenWidth - 200,
            timeout: 120000,
            frame: true,
            border: false,
            items: [
                {
                    xtype: 'fieldset',
                    defaults: {width: 1180},
                    title: '查询条件',
                    xtype: 'container',
                    border: true,
                    items: [
                        {
                            layout: 'column',
                            xtype: 'container',
                            items: [
                                {
                                    columnWidth: .15,
                                    layout: 'anchor',
                                    xtype: 'container',
                                    labelWidth: 30,
                                    items: [warehouse]
                                },
                                {
                                    columnWidth: .39,
                                    layout: 'anchor',
                                    xtype: 'container',
                                    items: [productCBB]
                                },
                                {
                                    columnWidth: .14,
                                    layout: 'anchor',
                                    xtype: 'container',
                                    items: [beginDate]
                                },
                                {
                                    columnWidth: .14,
                                    layout: 'anchor',
                                    xtype: 'container',
                                    items: [endDate]
                                }
                            ]
                        }
                    ]
                },
                {
                    xtype: 'fieldset',
                    defaults: {width: 1180},
                    title: '查询条件',
                    xtype: 'container',
                    border: true,
                    items: [
                        {
                            layout: 'column',
                            xtype: 'container',
                            items: [
                                {
                                    columnWidth: .15,
                                    layout: 'anchor',
                                    xtype: 'container',
                                    labelWidth: 30,
                                    items: [orderTypeArray]
                                },
                                {
                                    columnWidth: .13,
                                    layout: 'anchor',
                                    xtype: 'container',
                                    labelWidth: 30,
                                    items: [productType]
                                },
                                {
                                    columnWidth: .13,
                                    layout: 'anchor',
                                    xtype: 'container',
                                    items: [companies]
                                },
                                {
                                    columnWidth: .13,
                                    layout: 'anchor',
                                    xtype: 'container',
                                    items: [brand]
                                },
                                {
                                    columnWidth: .07,
                                    layout: 'anchor',
                                    xtype: 'container',
                                    items: [showBatchInfo]
                                },
                                {
                                    columnWidth: .05,
                                    layout: 'anchor',
                                    xtype: 'container',
                                    items: [searchButton]
                                },
                                {
                                    columnWidth: .05,
                                    layout: 'anchor',
                                    xtype: 'container',
                                    items: [clearButton]
                                },
                                {
                                    columnWidth: .1,
                                    layout: 'anchor',
                                    xtype: 'container',
                                    items: [exportXLSButton]
                                }
                            ]
                        }
                    ]
                }
            ]
        });

        //成本总值
        var costAmountLabel = Ext.create('Ext.form.Label', {
            width: 190,
            name: 'cost_amount_label'
        })
        costAmountLabel.setText("成本总值:0");

        //成本总值(含税)
        var taxAmountLabel = Ext.create('Ext.form.Label', {
            width: 190,
            name: 'tax_amount_label'
        })
        costAmountLabel.setText("成本总值:0");
        taxAmountLabel.setText("成本总值(含税):0");
        //查询结果列表
        Ext.define('StockModel', {
            extend: 'Ext.data.Model',
            fields: [
                {name: 'id'},
                {
                    name: 'order_number',
                    mapping: "resource",
                    convert: function (value, record) {
                        if (value == "") {
                            return "";
                        } else {
                            return value.order_number;
                        }
                    }
                },
                {name: 'product_number', mapping: 'product.number'},
                {name: 'product_name', mapping: 'product.name'},
                {name: 'begin_quantity', type: 'int'},
                {name: 'quantity', type: 'int'},
                {name: 'projected_quantity', type: 'int'},
                {name: 'warehouse_name', mapping: 'warehouse.name'},
                {name: 'description'},
                {name: 'product_cost_price', mapping: 'product.cost_price' },
                {name: 'action_date'},
                {name: 'created_at'}
            ]
        });

        var stockDS = Ext.create('Ext.data.Store', {
            //autoDestroy : true,
            model: 'StockModel',
            proxy: {
                type: 'ajax',
                url: '/stock/productQuery',
                reader: {
                    type: 'json',
                    root: 'root',
                    totalProperty: 'totalProperty'// 数据集记录总数
                }
            }
        });

        var toolBar = Ext.create('Ext.toolbar.Paging', {
            pageSize: pageSize + 10,
            store: stockDS,
            displayInfo: true,
            displayMsg: "显示第 {0} 条到 {1} 条记录，一共 {2} 条",
            emptyMsg: "没有记录"
        })

        var stockGrid = Ext.create('Ext.grid.Panel', {
            width: screenWidth - 170,
            height: 635,
            loadMask: true,
            store: stockDS,
            columns: [Ext.create('Ext.grid.RowNumberer'), {
                header: 'id',
                dataIndex: 'id',
                width: 55
            }, {
                header: '单号',
                dataIndex: 'order_number',
                width: 130
            }, {
                header: '产品名称',
                dataIndex: 'product_name',
                width: 240
            }, {
                header: '产品编号',
                dataIndex: 'product_number',
                width: 120
            }, {
                header: '起始库存',
                align: 'right',
                dataIndex: 'begin_quantity',
                width: 75
            }, {
                header: '准确库存',
                align: 'right',
                dataIndex: 'quantity',
                width: 75
            }, {
                header: '计划库存',
                align: 'right',
                dataIndex: 'projected_quantity',
                width: 75
            }, {
                header: '仓库名称',
                dataIndex: 'warehouse_name'
            }, {
                header: '成本均价',
                align: 'right',
                renderer: moneyPrivilegeRender,
                dataIndex: 'product_cost_price',
                width: 75
            }, {
                header: '描述',
                dataIndex: 'description',
                width: 150
            }, {
                header: '仓库操作日期',
                dataIndex: 'action_date',
                renderer: Ext.util.Format.dateRenderer('Y-m-d'), width: 90
            }, {
                header: '生成日期',
                dataIndex: 'created_at',
                renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'), width: 115
            }],
            //selModel :Ext.create('Ext.selection.CheckboxModel'),
            //enableHdMenu: false,
            stripeRows: true,
            title: '查询结果',
            frame: true,
            tbar: [taxAmountLabel, '-', costAmountLabel],
            bbar: toolBar
        });

        //总的panel
        var workbenchPanel = Ext.create('Ext.form.Panel', {
            width: screenWidth - 170,
            height: screenHeight - 230,
            autoScroll: true,
            frame: false,
            layout: {type: 'table', columns: 1},
            //bodyStyle: 'padding:15px;align:center',
            //listeners: {
            //expand: showAgileDiagram
            //},
            items: [queryFormPanel, stockGrid]
        });
        workbenchPanel.render("query_workbanch");
    })
</script>
</head>
<body>
<div id="query_workbanch"></div>
</body>
</html>
